/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.io.fasta;

import java.io.Writer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import phoside.PhosphoProteins;
import phoside.Protein;
import phoside.Proteins;

import phoside.data.util.PhosphoDataUtil;


/**
 *
 * @author gjj
 */
public class PhosphoWriterWithSitesInSequence
        extends DefaultFastaProteinsWriter {

    public PhosphoWriterWithSitesInSequence(Writer writer){
        this(writer, 80);
    }

    public PhosphoWriterWithSitesInSequence(Writer writer, int aaPerLine){
        super(writer, aaPerLine);
        setResidueAnnotator(new ResidueAnnotator() {
            public String annotate(Protein protein, int index) {
                return "[p"+protein.getSequence().charAt(index)+"]";
            }

            public TreeSet<Integer> indicesOfResidues(Protein protein) {
                Set<Integer> sites = PhosphoDataUtil.getSites(protein);
                if (sites==null) {
                    return new TreeSet();
                } else {
                    return new TreeSet(sites);
                }
            }
        });
    }

//    @Override
//    protected List<String> formatSequence(final Proteins data, final String acc) {
//        Protein protein = data.getProtein(acc);
//        if (protein==null) return null;
//
//        Set<Integer> tmp = ((PhosphoProteins)data).getSites(acc);
//        if (tmp==null || tmp.size()==0) {
//            return super.formatSequence(data, acc);
//        }
//
//        Set<Integer> sites = new TreeSet(tmp);
//
//        String sequence = data.getProtein(acc).getSequence();
//
//        int len = sequence.length();
//        int nseg = (len+aaPerLine-1)/aaPerLine;
//
//        List<String> seqs = new ArrayList();
//
//        StringBuilder sb = new StringBuilder(aaPerLine*2);
//
//        Iterator<Integer> it = sites.iterator();
//        int loc = it.next();
//        for (int iseg=0; iseg<nseg; iseg++) {
//            int sta = iseg*aaPerLine;
//            int end = (iseg+1)*aaPerLine;
//            if (end > len) {
//                end = len;
//            }
//
//            sb.setLength(0);
//            sb.append(sequence.substring(sta, end));
//
//            int nsites = 0;
//            while (sta<=loc && loc<end) {
//                int site = loc-sta+3*nsites;
//                nsites++;
//                sb.replace(site, site+1, "[p"+sb.charAt(site)+"]");
//
//                if (it.hasNext()) {
//                    loc = it.next();
//                } else {
//                    break;
//                }
//            }
//            seqs.add(sb.toString());
//        }
//
//        return seqs;
//    }
    
}
